﻿using AutoMapper;
using Core.SimpleTemp.Common;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Repositories;
using Core.SimpleTemp.Repositories.CBS;
using Core.Toolkit.Common;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Core.Toolkit.Core.Application;
using Core.Toolkit.Core.Model;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;


namespace Core.SimpleTemp.Application.CBS
{
    [AutoDi()]
    public class BlackContractorDirectoryAppService : BaseAppService<CBS_ListDto, CBS_List, BlackContractorDirectoryRepository>
    {
        readonly ExcelHelp _excelHelp;
        readonly UserIdentity _userIdentity;
        public BlackContractorDirectoryAppService(BlackContractorDirectoryRepository repository, ExcelHelp excelHelp, IHttpContextAccessor accessor) : base(repository)
        {
            _excelHelp = excelHelp;
            if (accessor.HttpContext != null)
            {
                _userIdentity = accessor.HttpContext.User.GetUserIdentity();
            }
        }

        private LoadPageOffsetModelAuto<CBS_List, CBS_ListDto>
             GetLoadPageOffsetMode(PagingModel<CBS_ListDto>
                 pagingQueryModel)
        {
            var loadPageOffsetModel = this.GetLoadPageOffsetModelAuto(pagingQueryModel);
            this.GetLoadPageOffsetModel(pagingQueryModel);

            //查询条件
            var whereEntity = pagingQueryModel.Model.ToEntity<CBS_List>();
            var where = this.CreateQRList()
              .Add("ContractorName", "in")
              .Add("ConstructionCategory", "=")
              .Add("status", "=")
              .Add("IsBack", "=")
              ;

            //工厂处理
            if (_userIdentity.Headoffice)
                where.Add(nameof(CBS_List.CreateFactory), "="); //总部从查询条件取工厂id
            else
                where.Add(nameof(CBS_List.CreateFactory), "=", _userIdentity.Factory);
            //基础查询，联表查询在此操作
            var baseQuery = _repository.QueryBase().AsNoTracking().Include(c => c.SysDepartment);
            loadPageOffsetModel.QueryWhere = where;
            loadPageOffsetModel.Selector = model => new CBS_List()
            {
                Id = model.Id,
                ContractorCode = model.ContractorCode,
                ContractorName = model.ContractorName,
                Arrdess = model.Arrdess,
                phone = model.phone,
                ConstructionCategory = model.ConstructionCategory,
                QualificationName = model.QualificationName,
                QualificationLevel = model.QualificationLevel,
                TechnicalStaffSituation = model.TechnicalStaffSituation,
                MainaAchievement = model.MainaAchievement,
                status = model.status,
                IsBack = model.IsBack,
                CreateFactory = model.CreateFactory,
                CreateDept = model.CreateDept,
                UpdateDept = model.UpdateDept,
                PrincipalName = model.PrincipalName,
                //Age = model.Age,
                IDNumber = model.IDNumber,
                SysDepartment = model.SysDepartment,
            };
            //按工厂过滤
            loadPageOffsetModel.BasqQuery = baseQuery.OrderByDescending(x => x.CreateTime);
            return loadPageOffsetModel;
        }

        public async Task<IPageModel<CBS_ListDto>> GetList(PagingModel<CBS_ListDto>
            pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            return await base.LoadPageOffsetAsync(loadPageOffsetModel);
        }


        public async Task<string>
            Export(PagingModel<CBS_ListDto>
               pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            var DataList = await this.ExportDataAsync(loadPageOffsetModel);
            var excelData = DataList.Select(model => new
            {
                model.ContractorCode,
                ContractorCode_title = "承包商代码",
                model.ContractorName,
                ContractorName_title = "承包商名称",
                model.ConstructionCategoryName,
                ConstructionCategoryName_title = "主要施工类别",
                model.CreateFactoryText,
                CreateFactoryText_title = "隶属工厂",
                model.SysDepartment.Name,
                Name_title = "主管部门",
                model.PrincipalName,
                PrincipalName_title = "负责人",
                //model.Age,
                //Age_title = "年龄",
                model.IDNumber,
                IDNumber_title = "身份证号",
                model.phone,
                phone_title = "电话",
                model.QualificationName,
                QualificationName_title = "资质名称",
                model.QualificationLevel,
                QualificationLevel_title = "资质等级",
                model.TechnicalStaffSituation,
                TechnicalStaffSituation_title = "技术人员情况",
                model.MainaAchievement,
                MainaAchievement_title = "主要业绩",

            }).ToList();
            return _excelHelp.Create(excelData);
        }


        public async Task<CBS_List> GetAsync(Guid id)
        {
            var basqQuery = _repository.QueryBase().AsNoTracking()
                .Include(c => c.SysDepartment)
            .Include(c => c.CBS_ListAttachments)
            .Include(c => c.CBS_ListAttachments).ThenInclude(u => u.SysUser).ThenInclude(u => u.SysUserInfor);
            var entity = await base.GetAsync(id, basqQuery);          
            return entity;
        }
    }
}
